package cn.ecnu.controller;


import cn.ecnu.constant.ResponseStatus;
import cn.ecnu.entity.Employee;
import cn.ecnu.service.EmployeeService;
import cn.ecnu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;

    @Autowired
    StringRedisTemplate template;

    static String COURIER_KEY = "courier_$_emission";
    static String TRANSMISSION_KEY = "transmission_$_emission";
    static String BUSY_SET = "busy_transmission_list";

    @GetMapping("/list")
    public R getEmployeeList() {
        List<Employee> result = employeeService.list();
        return R.ok().put("data", result);
    }

    @PostMapping("/insert")
    public R insertEmployee(@RequestBody Employee employee) {
        employee.setId(cn.ecnu.constant.ResponseStatus.uuidGenerator());
        boolean result = employeeService.save(employee);
        if (result) return R.ok().put("data", employee);
        return R.error(cn.ecnu.constant.ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @PostMapping("/update")
    public R updateEmployee(@RequestBody Employee employee) {
        boolean result = employeeService.updateById(employee);
        if (result) return R.ok().put("data", employee);
        return R.error(cn.ecnu.constant.ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @GetMapping("/{id}")
    public R getEmployeeById(@PathVariable("id") String id) {
        Employee employee = employeeService.getById(id);
        return R.ok().put("data", employee);
    }


    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") String id) {
        boolean result = employeeService.removeById(id);
        if (result) return R.ok();
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    // 身份是快递员
    @GetMapping("/courier/location/{location}")
    public R getEmployeeByLocation(@PathVariable String location) {
        List<Employee> holders = employeeService.list(new QueryWrapper<Employee>()
                .eq("location", location).eq("position", "快递员"));

        System.out.println(holders);

        SetOperations<String, String> ops = template.opsForSet();

        List<Employee> result = holders.stream().filter((holder) -> {
            return ops.size(COURIER_KEY.replace("$", holder.getId())) < 5;
        }).collect(Collectors.toList());

        return R.ok().put("data", result);

    }

    // 身份是运输员
    @GetMapping("/transmission/location/{location}")
    public R getTransmissionByLocation(@PathVariable String location) {
        List<Employee> holders = employeeService.list(new QueryWrapper<Employee>()
                .eq("location", location).eq("position", "运输员"));

        System.out.println(holders);

        SetOperations<String, String> ops = template.opsForSet();

        List<Employee> result = holders.stream().filter((holder) -> {
            return ops.size(TRANSMISSION_KEY.replace("$", holder.getId())) < 100 &&
                    !ops.isMember(BUSY_SET, holder.getId());
        }).collect(Collectors.toList());

        return R.ok().put("data", result);

    }
}
